﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel
{
    public partial class CheckReservation : Form
    {
        public CheckReservation()
        {
            InitializeComponent();
        }

        CNDatabase cnDb = new CNDatabase();

        private string getCustomer(string fieldSelect, string fieldCondition, string textbox)
        {
            try
            {
                string qr = "select top 1 " + fieldSelect + " from Customer where " + fieldCondition + " = '" + textbox + "'";
                return cnDb.getOneData(qr).ToString();
            }
            catch (Exception)
            {
                cnDb.closeConnect();
                return "";
            }
        }

        private void txtIdentify_TextChanged(object sender, EventArgs e)
        {
            grbInfoCus.Visible = true;
            txtName.Text = getCustomer("Fullname", "IdentificationNumber", txtIdentify.Text);
            txtPhone.Text = getCustomer("PhoneNumber", "IdentificationNumber", txtIdentify.Text);
            txtAddress.Text = getCustomer("Address", "IdentificationNumber", txtIdentify.Text);
            txtBirth.Text = getCustomer("BirthDay", "IdentificationNumber", txtIdentify.Text);
            try
            {
                string qr = "select COUNT(1) from BookingLog, ListRoomBooking "
                        + " where BookingLog.IDBookingLog=ListRoomBooking.IDBookingLog "
                        + " and IdentificationNumber='" + txtIdentify.Text + "'";
                txtPriority.Text = cnDb.getOneData(qr).ToString();

            }
            catch (Exception)
            {

            }
        }

        private void txtPhone_TextChanged(object sender, EventArgs e)
        {
            grbInfoCus.Visible = true;
            txtName.Text = getCustomer("Fullname", "PhoneNumber", txtPhone.Text);
            txtIdentify.Text = getCustomer("IdentificationNumber", "PhoneNumber", txtPhone.Text);
            txtAddress.Text = getCustomer("Address", "PhoneNumber", txtPhone.Text);
            txtBirth.Text = getCustomer("BirthDay", "PhoneNumber", txtPhone.Text);
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            btSaveUpdate.BringToFront();
            txtName.Enabled = true;
            txtBirth.Enabled = true;
            txtAddress.Enabled = true;
            txtIdentify.Enabled = false;
            txtPhone.Enabled = false;
        }

        private void btSaveUpdate_Click(object sender, EventArgs e)
        {
            btUpdate.BringToFront();
            txtName.Enabled = false;
            txtBirth.Enabled = false;
            txtAddress.Enabled = false;
            txtIdentify.Enabled = true;
            txtPhone.Enabled = true;
            try
            {
                DateTime birth = DateTime.Parse(txtBirth.Text);
                cnDb.connect();
                SqlCommand cmd = new SqlCommand("sp_updateCustomer", cnDb.cn);
                cmd.CommandText = "sp_updateCustomer";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Fullname", txtName.Text);
                cmd.Parameters.AddWithValue("@IdentificationNumber", txtIdentify.Text);
                cmd.Parameters.AddWithValue("@BirthDay", birth);
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@PhoneNumber", txtPhone.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Updated customer's information successfully !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                cnDb.closeConnect();
            }
            catch (Exception)
            {
                MessageBox.Show("Please enter complete customer's information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void txtIdentify_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Enter only numeric values in this field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                e.Handled = true;
            }
        }

        private void CheckReservation_Load(object sender, EventArgs e)
        {
            LbCurrentDay.Text = "Today is " + DateTime.Today.ToString("dd/MM/yyyy");
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            string qr = "select distinct * from BookingLog, ListRoomBooking, Room "
                    + " where BookingLog.IDBookingLog=ListRoomBooking.IDBookingLog "
                    + " and ListRoomBooking.IDRoom=Room.IDRoom "
                    + " and BookingLog.TypeBooking='Reservation' "
                    + " and IdentificationNumber= '" + txtIdentify.Text + "'";
            BindingSource bdS = new BindingSource();
            bdS.DataSource = cnDb.getDataset(qr).Tables[0];
            grvRoom.DataSource = bdS;
        }

        private void btCheckIn_Click(object sender, EventArgs e)
        {//CheckIn
            DataGridViewRow row = new DataGridViewRow();

            int numberRoom = 0;
            for (int i = 0; i < grvRoom.Rows.Count; i++)
            {
                row = grvRoom.Rows[i];
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                    {
                        numberRoom++;
                    }
            }
            if (numberRoom == 0)
                MessageBox.Show("Room not found, Reservation again please !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else
                {
                for (int i = 0; i < grvRoom.Rows.Count; i++)
                {
                    row = grvRoom.Rows[i];
                    string idBooking = Convert.ToString(row.Cells[0].Value);
                    string qr = "UPDATE [BookingLog] SET [TypeBooking] = 'Staying' WHERE IDBookingLog='" + idBooking + "'";
                    cnDb.xlExecuteNonQuery(qr);
                }
                MessageBox.Show("Congratulations! Checked in successfully ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
        }

       


    }
}


